#!/usr/bin/python
# -*- coding: utf8 -*- 
import cv
import time
import Image
class Weapon:
	
	weaponModel = None
	weapons = None
	Y = 0
	
	def __init__(self):
		self.weaponModel = cv.Load("hand.xml")

	def detectWeapon(self, image):
		min_size = (20,20)
		image_scale = 2
		haar_scale = 1.2
		min_neighbors = 2
		haar_flags = 0
		
		# Allocate the temporary images
		gray = cv.CreateImage((image.width, image.height), 8, 1)
		smallImage = cv.CreateImage((cv.Round(image.width / image_scale), cv.Round (image.height / image_scale)), 8 ,1)

		# Convert color input image to grayscale
		cv.CvtColor(image, gray, cv.CV_BGR2GRAY)

		# Scale input image for faster processing
		cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR)
		
		# Equalize the histogram
		cv.EqualizeHist(smallImage, smallImage)

		# Detect object
		self.weapons = cv.HaarDetectObjects(smallImage, self.weaponModel, cv.CreateMemStorage(0),
		haar_scale, min_neighbors, haar_flags, min_size)
		
		if self.weapons:
			print self.weapons
			self.Y = self.weapons[0][0][1]
			for ((x, y, w, h), n) in self.weapons:
				pt1 = (int(x * image_scale), int(y * image_scale))
				pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
				cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
				face_region = cv.GetSubRect(image,(x,int(y + (h/4)),w,int(h/2)))

			cv.SetImageROI(image, (pt1[0],
				pt1[1],
				pt2[0] - pt1[0],
				int((pt2[1] - pt1[1]) * 0.7)))
			
			
		cv.ResetImageROI(image)
		return image
		

	